package com.humngyang.com.hmy.study.concurrency.myannoations.concurrencyTest;
import com.humngyang.com.hmy.study.concurrency.myannoations.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

//线程不安全的
@ThreadSafe
public class ConcurrencyAtomicIntegerFieldUpdater {
    private static Logger log = LoggerFactory.getLogger(ConcurrencyAtomicIntegerFieldUpdater.class);
    private static AtomicIntegerFieldUpdater<ConcurrencyAtomicIntegerFieldUpdater> countNum =
            AtomicIntegerFieldUpdater.newUpdater(ConcurrencyAtomicIntegerFieldUpdater.class, "count");

    public volatile int count = 100;
    public int getCount() {
        return count;
    }
    private static ConcurrencyAtomicIntegerFieldUpdater aifu = new ConcurrencyAtomicIntegerFieldUpdater();
    public static void main(String[] args) {
        if (countNum.compareAndSet(aifu, 100, 120)) {
            log.info(" update success 1 {}", aifu.getCount());
        }
        if (countNum.compareAndSet(aifu, 100, 120)) {
            log.info(" update success 2 {}", aifu.getCount());
        } else {
            log.info(" update fail 1 {}", aifu.getCount());
        }
    }


}
